package com.lagou.boot;

import com.lagou.service.UserServiceImpl;
import org.apache.curator.RetryPolicy;
import org.apache.curator.framework.CuratorFramework;
import org.apache.curator.framework.CuratorFrameworkFactory;
import org.apache.curator.retry.ExponentialBackoffRetry;
import org.apache.zookeeper.CreateMode;

public class ServerBoot {

    private static String IP = "127.0.0.1";
    private static int PORT = 8999;
    private static String BASE_SPACE = "base";

    public static void main(String[] args) throws Exception {
        //启动服务器
        UserServiceImpl.startServer(IP,PORT);
        //创建zk客户端
        RetryPolicy retryPolicy = new ExponentialBackoffRetry(1000,3);
        CuratorFramework client = CuratorFrameworkFactory.builder()
                .connectString("127.0.0.1:2181")
                .sessionTimeoutMs(50000)
                .connectionTimeoutMs(30000)
                .retryPolicy(retryPolicy)
                .namespace(BASE_SPACE)
                .build();
        client.start();
        //创建临时节点，将服务ip和端口信息写入到节点信息中
        String path = "/lg-curator/c1";

        String s = client.create().creatingParentsIfNeeded()
                .withMode(CreateMode.EPHEMERAL).forPath(path, (IP + ":" + PORT).getBytes());
        System.out.println("服务端启动成功。。。");
    }
}
